package xyz.syyrjx.blog.controller.tool.game2048;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import xyz.syyrjx.blog.entity.Result;
import xyz.syyrjx.blog.entity.Score2048;
import xyz.syyrjx.blog.service.ToolService;
import xyz.syyrjx.blog.service.tools.Game2048Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2023/12/27 12:13
 * @description 2048游戏控制页面
 * @since v0.0.1
 */
@Controller
public class Game2048Controller {

    @Resource
    private Game2048Service game2048Service;

    @Resource
    private ToolService toolService;


    @RequestMapping("/tools/2048")
    public ModelAndView game2048Page(ModelAndView mv, HttpSession session) {
        if (session.getAttribute("igGame2048") == null) {
            toolService.updateView(2);
            session.setAttribute("ifGame2048",1);
        }
        mv.setViewName("/tools/2048");
        return mv;
    }

    @RequestMapping("/tools/2048/addScore")
    @ResponseBody
    public Result addScore(Score2048 score2048) {
        game2048Service.addScore(score2048);
        return Result.success(null);
    }

    @RequestMapping("/tools/2048/getRank")
    @ResponseBody
    public Result getRank(int score) {
        return Result.success(game2048Service.getRank(score));
    }

    @RequestMapping("/tools/2048/getScoreFirstFive")
    @ResponseBody
    public Result getScoreFirstFive() {
        return Result.success(game2048Service.getScoreFirstFive());
    }
}
